/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import biz.LoginProgess;
import da.exceptions.PreexistingEntityException;
import da.exceptions.RollbackFailureException;
import entities.Account;
import entities.Complain;
import entities.Reply;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author THOHCGC00177
 */
@ManagedBean
@SessionScoped
public class ReplyProgess {

    /**
     *
     * call ejb
     */
    @EJB
    private biz.ComplainProgess complainProgess;
    @EJB
    private biz.ReplyProgess replyProgess;
    @EJB
    private LoginProgess loginProgess;
    private String title;
    private String content;
    private String msg;

    /**
     * Function create a reply
     */
    public String createRely(int complainId, String name) {
        //get user login
        Account user = loginProgess.getUser(name);
        //get  complain is chosen
        Complain complainx = complainProgess.getComplainById(complainId);
        //create new date
        Date date = new Date();
        //Validate text box
        if("".equals(title)){
            msg = "please insert title!";
        }if("".equals(content)){
            msg = "please insert content";
        }
        try {
            //Creata a new reply
            Reply reply = new Reply(1);
            reply.setId(1);
            reply.setComplainId(complainx);
            reply.setTechnicalId(user);
            reply.setTitle(title);
            reply.setReply(content);
            reply.setDate(date);
            //Call ejb to create reply
            replyProgess.createReply(reply);
            //set new stt to complain
            complainx.setStatus("Complete");
            //call ejb to update status for complain
            complainProgess.updateStatus(complainx);
            msg = "Reply success!";
        } catch (PreexistingEntityException ex) {
            Logger.getLogger(ReplyProgess.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RollbackFailureException ex) {
            Logger.getLogger(ReplyProgess.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(ReplyProgess.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "";
    }
  

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public ReplyProgess() {
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}
